Efficiently Calculating Legendre Symbols

The following note describes an algorithm for computing Legendre symbols in a relatively efficient way. Note that because this requires integer factorisation, the method is not fast for very large numbers. Also note that these assume that we are working modulo a prime.


Illustrative Example

Example

Calculate

\[ \left(\frac{4950}{7919}\right).\]

We first verify that \(7919\) is prime. Now, we apply multiplicity by fully factorising \(4950\):

\[ \left(\frac{4950}{7919}\right) = \left(\frac{2 \cdot 3^3 \cdot 5 \cdot 17}{7919}\right) = \left(\frac{2}{7919}\right) \cdot \left(\frac{3^3}{7919}\right) \cdot \left(\frac{5}{7919}\right) \cdot \left(\frac{17}{7919}\right)\]

Now we can take out any even powers of any term, since these are trivially quadratic residues, in this case we just have \(3^2\) to remove:

\[\begin{align*} &\left(\frac{2}{7919}\right) \cdot \left(\frac{3^3}{7919}\right) \cdot \left(\frac{5}{7919}\right) \cdot \left(\frac{17}{7919}\right)\\ &= \left(\frac{2}{7919}\right) \cdot \left(\frac{3^2}{7919}\right) \cdot \left(\frac{3}{7919}\right) \cdot \left(\frac{5}{7919}\right) \cdot \left(\frac{17}{7919}\right) \\ &= \left(\frac{2}{7919}\right) \cdot \left(\frac{3}{7919}\right) \cdot \left(\frac{5}{7919}\right) \cdot \left(\frac{17}{7919}\right) \end{align*}\]

Now, we can explicitly evaluate the Legendre symbol of \(2\) using this result, and since \(7919 \equiv -1 \pmod 8\), \(2\) is a quadratic residue:

\[\begin{align*} &\left(\frac{2}{7919}\right) \cdot \left(\frac{3}{7919}\right) \cdot \left(\frac{5}{7919}\right) \cdot \left(\frac{17}{7919}\right) \\ &= \left(\frac{3}{7919}\right) \cdot \left(\frac{5}{7919}\right) \cdot \left(\frac{17}{7919}\right) \\ \end{align*}\]

For all the odd primes, we apply the law of quadratic reciprocity to flip these symbols. Note that \(7919 \equiv 3 \pmod 4\), and hence whether or not we need to negate the symbols depends only on the "numerator". In this case \(3\) is the only one which is \(3\) modulo \(4\), so we introduce one negative:

\[\begin{align*} &\left(\frac{3}{7919}\right) \cdot \left(\frac{5}{7919}\right) \cdot \left(\frac{17}{7919}\right) \\ &= -\left(\frac{7919}{3}\right) \cdot \left(\frac{7919}{5}\right) \cdot \left(\frac{7919}{17}\right) \\ \end{align*}\]

Then we reduce all the numerators modulo the denominators, which is valid since we are working in the same residue class.

\[\begin{align*} &-\left(\frac{7919}{3}\right) \cdot \left(\frac{7919}{5}\right) \cdot \left(\frac{7919}{17}\right) \\ &= -\left(\frac{2}{3}\right) \cdot \left(\frac{4}{5}\right) \cdot \left(\frac{14}{17}\right) \\ \end{align*}\]

and we are effectively back where we started, in the sense that each of these symbols can be evaluated using the method described here from top to bottom. That is, we explicitly evaluate the symbol of \(2\), we factorise any numerators, and we flip any symbols with prime numerators, until everything is reduced to something which is an obvious square, or \(2\) which we know how to evaluate explicitly.

It is however worth mentioning that at any point, if it is desired to work with negative numerators within the same residue class, we can pull out a \(-1\) using multiplicity, and then evaluate this explicitly using this result.


Generalised Results

This is an attempt to generalise the above, however the example above is chosen to include all of the possible useful results below, and hence is a better illustration of the process.

Theorem

To compute a Legendre symbol of \(a\) modulo \(p\) for prime number \(p\):

  1. If \(a = 0\) modulo \(p\), output \(0\).

    \(\)
    \left(\frac{0}{p}\right) = 0
    \(\)

  2. If \(a\) is greater than \(p\), reduce \(a\) modulo \(p\).

    \(\)
    \left(\frac{a}{p}\right) = \left(\frac{a \mod p}{p}\right)
    \(\)

  3. If \(a\) is a composite number, factorise \(a\) into a product of prime powers, explicitly extracting any even powers which have Legendre symbol of \(1\).

    \(\)

    \(\)

  4. If \(a\) is an odd prime, apply the quadratic law of reciprocity to flip the symbol.
  5. If \(a = 2\), explicitly calculate the Legendre symbol using this result.

At any point, if it is easier to work with a negative numerator in the same residue class modulo \(p\), this can be done by extracting the \(-1\) with multiplicativity and explicitly evaluating the Legendre symbol of \(-1\) using this result.

For each Legendre symbol that is remaining after applying a step in the above process, recursively apply this algorithm on each of these symbols.